package thinking_net;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class ChatServer {
	
	public static final int PORT=1711;
	private DatagramSocket socket;
	private byte[] bytes=new byte[1000];
	private DatagramPacket p=new DatagramPacket(bytes, bytes.length);

	public ChatServer() {
		
		try {
			socket=new DatagramSocket(PORT);
			System.out.println("server start :" +socket);
			while(true){
				socket.receive(p);
				String receiver_message=Dgram.toString(p)+"from address"+ p.getAddress()+" port "+p.getPort();
				System.out.println(receiver_message);
				String echoString="hello echo "+receiver_message;
				
				DatagramPacket echo = Dgram.toDatagram(echoString, p.getAddress(), p.getPort());
				socket.send(echo);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		new ChatServer();
	}
}
